package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// CouponBatch 优惠码批次
type CouponBatch struct {
	ID                  string  `gorm:"primaryKey;column:id;type:uint;size:64;comment:唯一ID" json:"id" json_old:"ID"`
	Name                string  `gorm:"column:name;type:varchar(60);comment:优惠码名称" json:"name"`
	CodeType            int     `gorm:"column:type;type:int;size:8;comment:优惠码类型,1通用码 2一人一码 3-兑换码" json:"code_type"`
	Code                string  `gorm:"column:code;type:string;size:20;comment:通用码号" json:"code"`
	Status              int     `gorm:"column:status;type:tinyint(1);default:1;comment:优惠码状态 1正常 2失效" json:"status"`
	TotalNum            int64   `gorm:"column:total_num;type:int;size:64;default:0;comment:发码数量" json:"total_num"`
	TotalUsedNum        int64   `gorm:"column:total_used_num;type:int;size:64;default:0;comment:已使用" json:"total_used_num"`
	TotalTimes          int64   `gorm:"column:total_times;type:int;size:32;default:1;comment:每个码使用的次数" json:"total_times"`
	ValidityStartTime   int64   `gorm:"column:validity_start_time;type:bigint;comment:开始时间" json:"validity_start_time"`
	ValidityEndTime     int64   `gorm:"column:validity_end_time;type:bigint;comment:结束时间" json:"validity_end_time"`
	OrderAmountRequired int     `gorm:"column:order_amount_required;type:tinyint(1);default:2;comment:是否有使用门槛:1 无使用门槛 2 需要消费满多少 " json:"order_amount_required"`
	OrderAmount         float64 `gorm:"column:order_amount;type:decimal(10,2);default:0.00;comment:消费金额满多少可以打折或者满减" json:"order_amount"`
	Reduce              float64 `gorm:"column:reduce;type:decimal(10,2);default:0.00;comment:满减的金额" json:"reduce"`
	Discount            float64 `gorm:"column:discount;type:decimal(10,2);default:10.00;comment:打折的折扣 " json:"discount"`
	Remark              string  `gorm:"column:remark;type:varchar(60);comment:优惠码内部备注" json:"remark"`
	Desc                string  `gorm:"column:desc;type:text;comment:使用说明" json:"desc"`
	MerchantID          string  `gorm:"column:merchant_id;type:int;size:64;index:idx_merchant_id;comment:所属商户id" json:"merchant_id"`
	TotalAmount         float64 `gorm:"column:total_amount;type:decimal(10,2);default:0.00;comment:总交易额" json:"total_amount"`
	TotalDiscountAmount float64 `gorm:"column:total_discount_amount;type:decimal(10,2);default:0.00;comment:总优惠额" json:"total_discount_amount"`
	AvgAmount           float64 `gorm:"column:avg_amount;type:decimal(10,2);default:0.00;comment:平均客单价" json:"avg_amount"`
	TotalOrderNum       int     `gorm:"column:total_order_num;type:int;size:64;comment:总订单量" json:"total_order_num"`
	CreatedAt           int64   `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
	UpdatedAt           int64   `gorm:"column:updated_at;autoUpdateTime;type:int;size:64;comment:修改时间" json:"updated_at"`
}

// TableName 表名称
func (cb CouponBatch) TableName() string {
	return "coupon_batch"
}

// BeforeCreate 自增id
func (cb *CouponBatch) BeforeCreate(tx *gorm.DB) (err error) {
	if cb.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	cb.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &CouponBatch{})
}
